﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using FluentRibbon;
using FluentRibbon.Definitions;
using FluentRibbon.Definitions.Controls;
using FluentRibbon.Libraries;
using SpWiki.Lib;
using SpWiki.Lib.Repository;
using SpWiki.Lib.Wiki;
using WikiPlex;
using WikiPlex.Formatting.Renderers;
using R = SpWiki.Layouts.SpWiki.Forms.Ribbon;

#endregion

namespace SpWiki.Layouts.SpWiki.Forms
{
    using Microsoft.SharePoint;

    public partial class View : RibbonLayoutsPage
   {
      #region Properties

      /// <summary>
      /// The wiki file.
      /// </summary>
      private SpWikiFile _file;

      /// <summary>
      /// The wiki file item
      /// </summary>
      private int _itemd = -1;

      /// <summary>
      /// The wiki library id.
      /// </summary>
      private string _spWikiLibrayUrl;

      /// <summary>
      /// Gets the wiki document file path.
      /// </summary>
      private string SpWikiLibrayUrl
      {
         get
         {
             if (!string.IsNullOrEmpty(_spWikiLibrayUrl)) return _spWikiLibrayUrl;
             if (!string.IsNullOrEmpty(Request.QueryString["SPWikiLibrayUrl"]))
             {
                 _spWikiLibrayUrl = Utils.DecodeHexString(Request.QueryString["SPWikiLibrayUrl"]);
             }
            return _spWikiLibrayUrl;
         }
      }

      /// <summary>
      /// The wiki library id.
      /// </summary>
      private Guid _listId;

      /// <summary>
      /// Gets the wiki library id.
      /// </summary>
      private Guid ListId
      {
         get
         {
            if (_listId != Guid.Empty) return _listId;

            if (!string.IsNullOrEmpty(Request.QueryString["SpWikiList"])) 
                _listId = new Guid(Request.QueryString["SpWikiList"]);

            if (!string.IsNullOrEmpty(SpWikiLibrayUrl))
            {
                _listId = Lib.Extensions.SpWebExtensions.GetListIdByUrl(SPContext.Current.Web, SpWikiLibrayUrl);
            }

            return _listId;
         }
      }

      /// <summary>
      /// Gets the wiki file item id.
      /// </summary>
      private int ItemId
      {
         get
         {
            if (_itemd != -1) return _itemd;
            _itemd = int.Parse(Request.QueryString["SpWikiID"]);
            return _itemd;
         }
      }

      /// <summary>
      /// Gets the wiki file.
      /// </summary>
      private SpWikiFile File
      {
         get { return _file ?? (_file = new SpWikiFile(ListId, ItemId)); }
      }

      #endregion  Properties

        protected void Page_Load(object sender, EventArgs e)
      {
      }

      /// <summary>
      /// Provide ribbon tab definition.
      /// </summary>
      /// <returns>
      /// If you return null here, tab will not be shown.
      /// Overwise, the ribbon tab is created and activated when the page is displayed.
      /// </returns>
      public override TabDefinition GetTabDefinition()
      {
         return new TabDefinition
                   {
                      Id = R.RibbonBarId,
                      Title = R.RibbonBarTitle,
                      Groups = new[]
                                  {
                                     new GroupDefinition
                                        {
                                           Id = R.GroupId,
                                           Title = R.GroupTitle,
                                           Template = GroupTemplateLibrary.SimpleTemplate,
                                           Controls = new ControlDefinition[]
                                                         {
                                                            new ButtonDefinition
                                                               {
                                                                  Id = R.ButtonEditId,
                                                                  Title = R.ButtonEditTitle,
                                                                  CommandJavaScript =
                                                                     "NavigateToUrl('" + File.EditUrl + "');",
                                                                  Image = ImageLibrary.GetStandardImage(3, 5),
                                                               }
                                                         }
                                        }
                                  }

                   };
      }

      /// <summary>
      /// Represents the method that handles the PreRender event.
      /// </summary>
      /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
      protected override void OnPreRender(EventArgs e)
      {
         base.OnPreRender(e);

         Macros.Register<TitleLinkMacro>();
         var engine = new WikiEngine();
         var output = engine.Render(File.RawContent, GetRenderers());

         PhWiki.Controls.Add(new LiteralControl("<h1>" + File.Title + "</h1>"));
         PhWiki.Controls.Add(new LiteralControl("<div>" + output + "</div>"));
      }

      /// <summary>
      /// Gets the renderers.
      /// </summary>
      /// <returns></returns>
      private IEnumerable<IRenderer> GetRenderers()
      {
         var titleLinkRenderer = new TitleLinkRenderer(new SpWikiLibrary(File.Library));
         var siteRenderers = new IRenderer[] {titleLinkRenderer};
         return Renderers.All.Union(siteRenderers);
      }

   }
}